package com.hymall.service.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hymall.bean.MarketCategory;
import com.hymall.bean.MarketCategoryExample;
import com.hymall.bean.common.CommonData;
import com.hymall.bean.vo.*;
import com.hymall.mapper.MarketCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: 周子伟
 * @description ${}
 * @date: 2022/7/18 9:10
 */
@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {

    @Autowired
    MarketCategoryMapper categoryMapper;

    /*
     * @author Invoker
     * @description {查询一级类目}
     * @date 2022/7/18 14:28
     * @param [level]
     * @return com.hymall.bean.common.CommonData
     */
    @Override
    public CommonData queryCategoryL1(String level) {
        PageHelper.startPage(1, 12);
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.setOrderByClause("add_time" + " " + "desc");
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        if (level != null && !"".equals(level)) {

            criteria.andLevelEqualTo(level);
        }
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(marketCategoryExample);
        CategoryL1Vo categoryL1Vo = new CategoryL1Vo();
        List<CategoryL1Vo> categoryL1VoList = new ArrayList<>();
        for (MarketCategory marketCategory : marketCategories) {
            categoryL1VoList.add(new CategoryL1Vo(marketCategory.getId(), marketCategory.getName()));
        }
        PageInfo<CategoryL1Vo> pageInfo = new PageInfo<>(categoryL1VoList);
        pageInfo.setTotal(12);
        pageInfo.setPages(1);
        pageInfo.setPageSize(12);
        pageInfo.setPageNum(1);
        CommonData data = CommonData.data(pageInfo);
        return data;
    }


    /*
     * @author Invoker
     * @description {实现显示商品类目功能}
     * @date 2022/7/18 18:44
     * @param [level]
     * @return com.hymall.bean.common.CommonData
     */
    @Override
    public CommonData queryCategory(String level) {

        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        if (level != null && !"".equals(level)) {

            criteria.andLevelEqualTo(level);
        }
        //将一级类目L1查出来,封装到   List<MarketCategory> marketCategories 之后嵌套for循环 查二级类目
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(marketCategoryExample);
        ;
        List<CategoryVo> list = new ArrayList<>();
        List<CategoryListVo> categoryListVos = new ArrayList<>();
        for (MarketCategory marketCategory : marketCategories) {
            //因为二级类目的pid =一级类目的id  一个一级类目下有多个二级类目 用list封装
            List<CategoryChildrenVo> childRen = categoryMapper.selectByPid(marketCategory.getId());

            // categoryListVo对象其实是包含 成员变量 和list <CategoryChildrenVo>children 的对象
            CategoryListVo<CategoryChildrenVo> categoryListVo = new CategoryListVo<>(marketCategory.getId(),
                    marketCategory.getName(), marketCategory.getKeywords(), marketCategory.getDesc(), marketCategory.getIconUrl()
                    , marketCategory.getPicUrl(), marketCategory.getLevel(), childRen);
            //每次循环new出来一个对象 并且 将对象封装到list里
            categoryListVos.add(categoryListVo);

        }
        PageInfo<CategoryListVo> pageInfo = new PageInfo<>(categoryListVos);
        pageInfo.setTotal(12);
        pageInfo.setPages(1);
        pageInfo.setPageSize(12);
        pageInfo.setPageNum(1);
        CommonData data = CommonData.data(pageInfo);


        return data;
    }


    /*
     * @author Invoker
     * @description {新增类目功能}
     * @date 2022/7/18 18:45
     * @param [marketCategory]
     * @return com.hymall.bean.vo.CategoryCreateVo
     */
    @Override
    public CategoryCreateVo create(MarketCategory marketCategory) {
        if ((marketCategory.getPid() != 0)&& marketCategory.getPid() != marketCategory.getId()) {

            categoryMapper.insertSelective(marketCategory);

        }
            Date addtime = new Date();

            CategoryCreateVo categoryCreateVo = new CategoryCreateVo(marketCategory.getId(), marketCategory.getName(),
                    marketCategory.getKeywords(), marketCategory.getDesc(), marketCategory.getPid(), marketCategory.getIconUrl(), marketCategory.getPicUrl(),
                    marketCategory.getLevel(), addtime, addtime);


        return categoryCreateVo;
    }

    /*
     * @author Invoker
     * @description {修改类目}
     * @date 2022/7/18 19:34
     * @param [marketCategory]
     * @return boolean
     */
    @Override
    public boolean update(MarketCategory marketCategory) {

        if ((marketCategory.getPid() != 0)&& marketCategory.getPid() != marketCategory.getId()) {

            Date updatetime = new Date();

            categoryMapper.updateByPrimaryKeySelective(marketCategory);
            marketCategory.setUpdateTime(updatetime);


        }

        return true;
    }


    /*
     * @author Invoker
     * @description {删除类目}
     * @date 2022/7/18 19:34
     * @param [marketCategory]
     * @return boolean
     */
    @Override
    public boolean delete(MarketCategory marketCategory) {
        Integer id = marketCategory.getId();
        categoryMapper.deleteByPrimaryKey(id);
        return true;
    }


}
